/*
 * @Author: your name
 * @Date: 2021-12-17 15:26:00
 * @LastEditTime: 2022-05-11 19:41:17
 * @LastEditors: 陈宇 975155172@qq.com
 * @Description: 定义数据库的参数和数据类型
 * @FilePath: \blog-api\src\entities\user.entity.ts
 */

import { Column, Entity, PrimaryGeneratedColumn, BaseEntity, ManyToMany, JoinTable, RelationId } from 'typeorm';
import { Tags } from '../../tags/entities/tags.entity';
import { Categories } from '../../categories/entities/categories.entity';

@Entity('abstract')
export class Abstract extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({type:'int', name: 'user_id', comment: '用户ID(谁添加的)', nullable: true, default: 0})
  user_id: number;

  @Column({type:'varchar', name: 'title', comment: '文章标题'})
  title: string;

  @Column({type:'mediumtext', name: 'content', comment: '文章内容'})
  content: string;

  @Column({type:'varchar', name: 'author', comment: '作者'})
  author: string;

  @Column({type:'varchar', name: 'categories', comment: '分类', length: 20})
  categories: string;

  @Column({type:'varchar', name: 'tags', comment: '标签', default: '[]'})
  tags: string;

  @ManyToMany(() => Categories, category => category.id)
  @JoinTable({ name: 'abstract__categories' })
  category: Categories[];

  @RelationId((abstract: Abstract) => abstract.category) // 需要指定目标关联关系
  category_id: number

  @ManyToMany(() => Tags, tag => tag.id)
  @JoinTable({ name: 'abstract__tags' })
  tag: Tags[];

  @RelationId((abstract: Abstract) => abstract.tag) // 需要指定目标关联关系
  tag_id: number

  @Column({type:'int', name: 'comment_num', comment: '评论数', default: 0})
  comment_num: number;

  @Column({type:'varchar', name: 'describe_content', comment: '描述'})
  describe_content: string;

  @Column({type:'int', name: 'see_num', comment: '查看次数', default: 0})
  see_num: number;

  @Column({type:'int', name: 'praise', comment: '点赞次数', default: 0})
  praise: number;

  @Column({type:'boolean', name: 'is_show', comment: '是否显示文章', default: true})
  is_show: boolean;

  @Column({type:'boolean', name: 'is_delete', comment: '文章是否被删除', default: false})
  is_delete: boolean;

  @Column({
    name: 'add_time',
    type: 'timestamp',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  add_time: Date;

  @Column({
      name: 'editor_time',
      type: 'timestamp',
      comment: '更新时间',
      default: () => 'CURRENT_TIMESTAMP',
      onUpdate: 'CURRENT_TIMESTAMP',
  })
  editor_time: Date;
}